<template>
  <el-row>
    <el-col :span="24" class="bg1">
      <div class="grid-content bg-purple">
        <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="110" :isSubmitBtn="false" @formChange="changeHandle" />
      </div>
    </el-col>
    <el-col :span="24">
      <div class="btn">
        <el-button size="small" icon="el-icon-folder-add" type="primary" @click="query">确定</el-button>
        <el-button size="small" icon="el-icon-refresh-left" type="primary" @click="reset">取消</el-button>
      </div>
    </el-col>
    <BaseDialog :visible.sync="visiblNewaddwxxmfilter" title="维修项目编辑" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '10px' }" destroyOnClose>
      <newAddwxxmfilter ref="newAddwxxmfilter" @closeNewaddwxxmfilter="closeNewaddwxxmfilter" />
      <el-row> </el-row>
    </BaseDialog>
  </el-row>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getModelGroup, getPrimaryGroupNameList, getLAbourCode, PosNewAddwxxm } from '@/api/repair/repair/activitesManage';
import newAddwxxmfilter from './newAddwxxmfilter';
import pinyin, { STYLE_FIRST_LETTER } from '@/components/Pinyin/index';
export default {
  name: 'newAddwxxm',
  components: {
    newAddwxxmfilter
  },
  mixins: [dictionary],
  data() {
    return {
      visiblNewaddwxxmfilter: false,
      formList: this.createFormList(),
      fetchParams: { xhrAbort: false },
      // fetchApi: getOrderList,
      onChangeColumn: [],
      formData: []
    };
  },
  mounted() {
    this.getModelGroup();
    this.getPrimaryGroupNameList();
    this.getLAbourCode();
  },
  methods: {
    validateFn(rule, value, callback) {
      if (value.length < rule.limit) {
        return callback(new Error(rule.message));
      }
      callback();
    },
    closeNewaddwxxmfilter(val) {
      if (val != false) {
        let a = [];
        console.log(val);
        val.forEach(x => {
          a.push(x.modelLabourCode);
        });
        a.push(this.formData.modelGroup);
        this.formData.modelLabourCode = a.join(',');
        this.PosNewAddwxxm(this.formData);
        this.$emit('closeNewwx', false);
      }
      this.visiblNewaddwxxmfilter = false;
    },
    // 提交表单获取子弹框数据的方法
    async PosNewAddwxxm(val) {
      const res = await PosNewAddwxxm(val);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '保存成功！' });
      }
    },
    // 项目车系下拉框获取方法
    async getModelGroup() {
      const res = await getModelGroup();
      this.formList.find(x => x.fieldName === 'modelGroup').itemList = res.data.map(x => ({ value: x.MODEL_LABOUR_CODE, text: x.MODEL_LABOUR_NAME }));
    },
    // 主分类名称下拉框获取方法
    async getPrimaryGroupNameList() {
      const res = await getPrimaryGroupNameList();
      this.formList.find(x => x.fieldName === 'primaryGroupName').itemList = res.data.map(x => ({ value: x.MAIN_GROUP_CODE, text: x.MAIN_GROUP_NAME }));
    },
    // 项目维修代码下拉框获取方法
    async getLAbourCode() {
      const res = await getLAbourCode();
      this.formList.find(x => x.fieldName === 'REPAIR_TYPE_CODE').itemList = res.data.map(x => ({ value: x.REPAIR_TYPE_CODE, text: x.REPAIR_TYPE_NAME }));
    },
    query() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    reset() {
      this.$emit('closeNewwx', false);
    },
    changeHandle(val) {
      this.visiblNewaddwxxmfilter = true;
      console.log(val, 'val');
      this.formData = val;
      console.log(this.formData, 'this.formData');
    },
    handleHour() {
      this.formList.find(x => x.fieldName === 'partName').initialValue = this.formList.find(x => x.fieldName === 'STD_LABOUR_HOUR').initialValue;
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '项目车系',
          fieldName: 'modelGroup',
          itemList: [],
          placeholder: '请选择',
          initialValue: '',
          rules: [{ required: true, message: '请输入项目车系', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '主分类名称',
          fieldName: 'primaryGroupName',
          itemList: [],
          placeholder: '请选择',
          initialValue: '',
          rules: [{ required: true, message: '请输入主分类名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '维修项目代码',
          fieldName: 'LABOUR_CODE',
          placeholder: '',
          initialValue: '',
          rules: [{ required: true, message: '请输入维修项目代码', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '项目维修类型',
          fieldName: 'REPAIR_TYPE_CODE',
          itemList: [],
          placeholder: '请选择',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '维修项目名称',
          fieldName: 'LABOUR_NAME',
          placeholder: '请选择',
          initialValue: '',
          onInput: e => {
            this.formList.find(x => x.fieldName == 'SPELL_CODE').initialValue = pinyin(e, { style: STYLE_FIRST_LETTER })
              .flat()
              .join('');
          },
          rules: [{ required: true, message: '请输入维修项目名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '拼音代码',
          fieldName: 'SPELL_CODE',
          placeholder: '',
          initialValue: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '工种',
          itemList: this.createDictList('8041'),
          fieldName: 'WORKER_TYPE_CODE',
          placeholder: '请选择',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '标准工时',
          fieldName: 'STD_LABOUR_HOUR',
          placeholder: '',
          initialValue: '0',
          unitRender: () => {
            return (
              <div style="cursor: pointer;" onClick={() => this.handleHour()}>
                =
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '派工工时',
          fieldName: 'partName',
          placeholder: '',
          initialValue: '0'
        },
        {
          type: 'INPUT',
          label: '索赔工时',
          fieldName: 'CLAIM_LABOUR',
          placeholder: '',
          initialValue: '0'
        },
        {
          type: 'INPUT_NUMBER',
          label: '总部标准工时',
          fieldName: 'OEM_LABOUR_HOUR',
          placeholder: '',
          precision: 2,
          initialValue: '0'
        }
      ];
    }
  }
};
</script>

<style scoped lang="less">
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}
.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}
.btn {
  text-align: center;
  margin: 15px;
}
.btnStyle {
  margin-top: 10px;
}
</style>
